import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

# 静态条形图
values = [10, 20, 30, 40]
labels = ["A", "B", "C", "D"]
plt.bar(labels, values)
plt.show()

# 静态扇形图
values = np.array([10, 20, 30, 40])
labels = ["A", "B", "C", "D"]
explode = [0, 0.1, 0, 0]  # 设置某个部分分离
plt.pie(values, explode=explode, labels=labels)
plt.show()


# 动态条形图
def update(frame):
    values = np.random.rand(4)  # 生成随机数作为新的数据
    rectangles.set_height(values)
    return rectangles


fig, ax = plt.subplots()
values = np.random.rand(4)  # 初始数据
labels = ["A", "B", "C", "D"]
rectangles = ax.bar(labels, values)

# 使用animation.FuncAnimation更新图形
anim = animation.FuncAnimation(fig, update, interval=500)
plt.show()
